package com.autotest.glin.common;

import com.autotest.glin.util.Constant;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.testng.Reporter;

import java.util.Map;

/**
 * get请求相关
 */
public class RunCaseGet {


    /**
     * 不带 header 的 get 请求
     *
     * @param url
     * @param path
     * @param params
     * @return
     */
    public Response get(String url, String path, Map<String, Object> params) {
        Reporter.log("Get request url: " + url + "  path:" + path);
        Reporter.log("    request params:" + params);
        Response response = RestAssured.given().baseUri(url).request().params(params).get(path);
        if (response.statusCode() != Constant.STATE_CODE_OK) {
            Reporter.log("request response: " + response.getBody().asString());
        }
        Reporter.log("    response: " + response.asString());
        return response;
    }

    /**
     * 带 header 的get 请求
     *
     * @param url
     * @param path
     * @param params 参数
     * @return Response
     */
    public Response get(String url, String path, Map<String, Object> params, Map<String, Object> headers) {
        Reporter.log("Get    request url: " + url + "  path:" + path);
        Reporter.log(String.valueOf(headers));
        Reporter.log("       request params:" + params);

        Response response = RestAssured.given().baseUri(url).request().params(params).headers(headers).get(path);

        //如果状态码不是200 这打印异常日志
        if (response.statusCode() != Constant.STATE_CODE_OK) {
            Reporter.log("   request response: " + response.getBody().asString());
        }
        Reporter.log("     response: " + response.asString());
        return response;
    }

    /**
     * get 请求 无body
     *
     * @param url
     * @param path
     * @return Response
     */
    public Response get(String url, String path) {
        Reporter.log("Get    request url: " + url + "  path:" + path);
        Response response = RestAssured.given().baseUri(url).request().get(path);

        //如果状态码不是200 这打印异常日志
        if (response.statusCode() != Constant.STATE_CODE_OK) {
            Reporter.log("   request response: " + response.getBody().asString());
        }
        Reporter.log("     response: " + response.asString());
        return response;
    }
}
